<?php

/**
 * This is the model class for table "artist".
 *
 * The followings are the available columns in table 'artist':
 * @property string $id
 * @property string $name
 * @property string $io
 * @property string $website
 * @property integer $category
 */
class Artist extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Artist the static model class
     */
    public $show_home;
	public $home_show;
	public $type = 'artist';
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'artist';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name,category, home_show', 'required'),
            array('category,show_home', 'numerical', 'integerOnly' => true),
            array('name, website', 'length', 'max' => 255),
            array('website','url'),
            array('io', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, tag, io, website, category', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'category_name' => array(self::BELONGS_TO, 'ArtistCategory', 'category'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'io' => 'Bio',
            'website' => 'Website',
            'category' => 'Category',
            'category_name' => 'Category',
            'tag' => 'Tags',
            'show_home' => 'Homepage Position',
			'home_show' => 'Show On Front Page'
        );
    }
    public function afterSave() {
        if($this->show_home != null)
        {
            $home_model = Home::model()->findByPk($this->show_home);
            $home_model->type = 'artist';
            $home_model->owner = $this->id;
            $home_model->save();
        }
        $this->addSearch();
    }
    public function addSearch() {
        $search_model = SearchAll::model()->findByAttributes(array('type' => 'artist', 'real_id' => $this->id));
        if ($search_model) {
            $search_model->short = $this->name;
            $search_model->save();
        }else{
            $search_model = new SearchAll;
            $search_model->short = $this->name;
            $search_model->type = 'artist';
            $search_model->real_id = $this->id;
            $search_model->save();
        }
    }
    public function getThumbnail(){
        $project = ArtistProject::model()->findByAttributes(array('artist'=>$this->id));
        if($project != null){
            return $project->thumbnail;
        }
        return null;
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('io', $this->io, true);
        $criteria->compare('website', $this->website, true);
        $criteria->compare('category', $this->category);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}